package com.ma.home;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ma.R;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by loma on 2017/10/11.
 */

public class DownloadFeesDialog extends AlertDialog {
    public DownloadFeesDialog(Context context) {
        super(context);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setContentView(view);

        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        initView();

    }

    private Button button;
    private TextView textView,title;
    private ImageView imageView;
    View view;

    private void initView() {

        view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_download_fees, null);

        ImageView close = (ImageView) view.findViewById(R.id.iv_close);
        imageView = (ImageView) view.findViewById(R.id.imageView);

        button = (Button) view.findViewById(R.id.button);
        textView = (TextView) view.findViewById(R.id.textView);
        title = (TextView) view.findViewById(R.id.title);


        button.setOnClickListener(view1 -> {
            imageView.setVisibility(View.VISIBLE);
            imageView.setBackgroundResource(R.mipmap.icon_sha_lou);
            textView.setText("正在下载，请稍等");
            button.setEnabled(false);
            title.setText("下载进度");
            button.setText("确定");
            setContentView(view);

            Observable
                    .timer(3000, TimeUnit.MILLISECONDS)
//                .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> {
                        imageView.setBackgroundResource(R.mipmap.tong_bu_wan_cheng);
                        textView.setText("下载完成");

                        button.setEnabled(true);
                        setContentView(view);

                    });

        });
        close.setOnClickListener(view1 -> {
            dismiss();
        });


    }


}
